package com.example.smartcitydemo.util

import android.util.Log
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.BindingAdapter
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.example.smartcitydemo.IP
import com.example.smartcitydemo.MyApplication
import com.example.smartcitydemo.R
import com.example.smartcitydemo.ext.onLoad

/**
 * @PACKAGE_NAME: com.example.smartcitydemo.util
 * @NAME: Caleb.F
 * @DATE: 2021/11/5 16:53
 * @Email: 1919796283@qq.com
 * dataBinding的扩展 注解
 */

@BindingAdapter("app:imgUrl")
fun onLoad(imageView: ImageView, url: String? = "") {
    val imageViewUrl = "${MyApplication.sp.getString(IP, "http://124.93.196.45:10001")}$url"
    imageView.run {
        Glide.with(this).load(imageViewUrl).placeholder(R.drawable.loading)
            .error(R.drawable.picfail)
            .into(this)
    }
}

    @BindingAdapter("app:onBack")
    fun onBackPressed(imageView: ImageView, info: Boolean) {
        imageView.setOnClickListener {
            if (it.context is AppCompatActivity) {
                (it.context as AppCompatActivity).onBackPressed()
            }
        }
}

@BindingAdapter("app:avatar")
fun onLoadAvatar(imageView: ImageView, url: String? = "") {
    //需要加上/prod-api才可正常
    val imageViewUrl =
        "${MyApplication.sp.getString(IP, "http://124.93.196.45:10001")}/prod-api/$url"
    imageView.run {
        Glide.with(this).load(imageViewUrl).apply(RequestOptions.circleCropTransform())
            .placeholder(R.drawable.loading)
            .error(R.drawable.picfail)
            .into(this)
    }
}


